//
//  UpdateNameViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/30.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "UpdateNameViewController.h"

@interface UpdateNameViewController ()

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, weak)UIView * backView;

@property (nonatomic, weak)UILabel * nameLabel;

@property (nonatomic, weak)UITextField * nameTextFiled;

@property (nonatomic, weak)UILabel * explationLabel;

@property (nonatomic, weak)UIButton * saveButton;

@end

@implementation UpdateNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
}

- (void)setupNavBar{
    
    self.title = @"修改昵称";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = [UIColor whiteColor];
    backView.alpha = 0.05;

    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    
    UILabel * nameLabel = [[UILabel alloc] init];
    
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameLabel.textColor = ColorWithRGB(0xffffff);
    nameLabel.text = @"昵称";
    
    self.nameLabel = nameLabel;
    
    [self.view addSubview:self.nameLabel];
    
    
    UITextField * nameTextFiled = [[UITextField alloc] init];
    
    nameTextFiled.borderStyle = UITextBorderStyleNone;
    nameTextFiled.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    nameTextFiled.tintColor = ColorWithRGB(0x53565c);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x53565c);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入新的昵称" attributes:attrs];
    nameTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameTextFiled.textColor = ColorWithRGB(0x53565c);
    nameTextFiled.keyboardType = UIKeyboardTypeDefault;
    nameTextFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.nameTextFiled = nameTextFiled;
    
    [self.view addSubview:self.nameTextFiled];
    
    
    UILabel * explationLabel = [[UILabel alloc] init];
    
    explationLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
    explationLabel.textColor = ColorWithRGB(0xee4b4e);
    explationLabel.text = @"*限制4-16个字符，2-8个汉字";
    
    self.explationLabel = explationLabel;
    
    [self.view addSubview:self.explationLabel];
    
    
    UIButton * saveButton = [[UIButton alloc] init];
    
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16 * SCREENH6];//
    [saveButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    saveButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
//    saveButton.layer.borderWidth =1.0f;
    //处理圆角
    saveButton.layer.cornerRadius =5.0f;
    [saveButton setTitleColor:ColorWithRGB(0xffffff) forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(didSaveButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.saveButton = saveButton;
    
    [self.view addSubview:self.saveButton];
}



- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.top.mas_equalTo(self.view);
    }];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(88 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
    }];
    
    [self.nameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(60 * SCREENH6);
        make.height.mas_equalTo(self.nameLabel);
        make.width.mas_equalTo(150 * SCREENH6);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView.mas_bottom).offset(3 * SCREENH6);
        make.leading.mas_equalTo(self.backView);
    }];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView.mas_bottom).offset(41 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.backView);
        make.height.mas_equalTo(46 * SCREENH6);
    }];
}



- (void)didSaveButton{
    
    [self.nameTextFiled resignFirstResponder];
    
    if ([self.nameTextFiled.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"昵称不能为空,请输入新的昵称"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  NSLog(@"action = %@", action);
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
        }else{
        
            NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
            
            [userDefaults setObject:self.nameTextFiled.text forKey:@"nickNameString"];
            
            [userDefaults synchronize];
            
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            NSDictionary * parametersDict = @{@"nickname":self.nameTextFiled.text};
            
            [[NetworkTool sharedTools] POST:@"user/edit-nickname" parameters:parametersDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                
                NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                
                if ([code isEqualToString:@"0"]) {
                    
                    self.callBackBlock(self.nameTextFiled.text);
                    
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                   message:@"恭喜你，修改昵称成功！"
                                                                            preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                          handler:^(UIAlertAction * action) {
                                                                              //响应事件
                                                                              [self.navigationController popViewControllerAnimated:YES];
                                                                          }];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                    
                }UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                message:responseObject[@"msg"]
                                                                         preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                NSLog(@"%@",error);
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"网络连接失败！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }];
        
    }
    
    
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
